package com.googlecode.jascii.ap;



import info.monitorenter.cpdetector.CharsetPrinter;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;




public class TextPrinterAp extends JFrame {
	
	private TextFileComponent txtComponent;
	private PageFormat pageFormat;
	private String title;
	//private String encode  = "MS950";
	
	
	public static void main(String[] args) {
		    
		    new TextPrinterAp();
		  }
	public TextPrinterAp() {
	    super("TextPrinterAp v1.0");
	    createUI();
	    PrinterJob pj = PrinterJob.getPrinterJob();
	    pageFormat = pj.defaultPage();
	    setVisible(true);
	  
	  }
	protected void createUI() {
	    setSize(600, 900);
	    Container content = getContentPane();
	    content.setLayout(new BorderLayout());

	    // Add the menu bar.
	    JMenuBar mb = new JMenuBar();
	    JMenu file = new JMenu("File", true);
	    file.add(new FileOpenAction()).setAccelerator(
	        KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.CTRL_MASK));
	    file.add(new FilePrintAction()).setAccelerator(
	        KeyStroke.getKeyStroke(KeyEvent.VK_P, Event.CTRL_MASK));
	    file.add(new FilePageSetupAction()).setAccelerator(
	        KeyStroke.getKeyStroke(KeyEvent.VK_P,
	             Event.CTRL_MASK | Event.SHIFT_MASK));
	    file.addSeparator();
	    file.add(new FileQuitAction()).setAccelerator(
	        KeyStroke.getKeyStroke(KeyEvent.VK_Q, Event.CTRL_MASK));
	    mb.add(file);
	    JMenu page = new JMenu("Page", true);
	    page.add(new PageNextPageAction()).setAccelerator(
	        KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 0));
	    page.add(new PagePreviousPageAction()).setAccelerator(
	        KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 0));
	    mb.add(page);
	    setJMenuBar(mb);
	    
	    // Add the contents of the window.
	    getContentPane().setLayout(new BorderLayout());

	    // Exit the application when the window is closed.
	    addWindowListener(new WindowAdapter() {
	      public void windowClosing(WindowEvent e) {
	        System.exit(0);
	      }
	    });
	
	
	}
	private void LocationCenter()
	{
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	    Dimension frameSize = getSize();
	    int x = (screenSize.width - frameSize.width) / 2;
	    int y = (screenSize.height - frameSize.height) / 2;
	    setLocation(x, y);
	    Container content = getContentPane();
	    content.setLayout(new BorderLayout());
	}
	public void showTitle() {
	    int currentPage = txtComponent.getCurrentPage();
	    int numPages = txtComponent.getNumPages();
	    setTitle(title + " - page " + currentPage + " of " + numPages);
	  }

	  public class FileOpenAction
	      extends AbstractAction {
	    public FileOpenAction() { super("Open..."); }
	    public void actionPerformed(ActionEvent ae) {
	      // Pop up a file dialog.
	      JFileChooser fc = new JFileChooser(".");
	      int result = fc.showOpenDialog(TextPrinterAp.this);
	      if (result != 0) { return; }
	      java.io.File f = fc.getSelectedFile();
	      
	      if (f == null) { return; }
	      // Load the specified file.
	      try {
	    	  
	    	  CharsetPrinter choose = new CharsetPrinter();
	    	  String encode = choose.guessEncoding(f);
	    	  if("BIG5".equalsIgnoreCase(encode))encode = "MS950";
	    	  txtComponent = new TextFileComponent(f, pageFormat,encode);
	        title = "[" + f.getName() + "]";
	        showTitle();
	        JScrollPane jsp = new JScrollPane(txtComponent);
	        getContentPane().removeAll();
	        getContentPane().add(jsp, BorderLayout.CENTER);
	        validate();
	      }
	      catch (java.io.IOException ioe) {
	        System.out.println(ioe);
	      }
	    }
	  }
	 
	  public class FilePrintAction
	      extends AbstractAction {
	    public FilePrintAction() { super("Print"); }
	    public void actionPerformed(ActionEvent ae) {
	    	
	      PrinterJob pj = PrinterJob.getPrinterJob();
	      pj.setPrintable(txtComponent, pageFormat);
	      if (pj.printDialog()) {
	        try { pj.print(); }
	        catch (PrinterException e) {
	          System.out.println(e);
	        }
	      }
	    }
	  }
	 
	  public class FilePageSetupAction
	      extends AbstractAction {
	    public FilePageSetupAction() { super("Page setup..."); }
	    public void actionPerformed(ActionEvent ae) {
	      PrinterJob pj = PrinterJob.getPrinterJob();
	      pageFormat = pj.pageDialog(pageFormat);
	      if (txtComponent != null) {
	        txtComponent.paginate(pageFormat);
	        showTitle();
	      }
	    }
	  }
	  
	  public class FileQuitAction
	      extends AbstractAction {
	    public FileQuitAction() { super("Quit"); }
	    public void actionPerformed(ActionEvent ae) {
	      System.exit(0);
	    }
	  }
	  
	  public class PageNextPageAction
	      extends AbstractAction {
	    public PageNextPageAction() { super("Next page"); }
	    public void actionPerformed(ActionEvent ae) {
	      if (txtComponent != null) txtComponent.nextPage();
	      showTitle();
	    }
	  }
	  
	  public class PagePreviousPageAction
	      extends AbstractAction {
	    public PagePreviousPageAction() { super("Previous page"); }
	    public void actionPerformed(ActionEvent ae) {
	      if (txtComponent != null) txtComponent.previousPage();
	      showTitle();
	    }
	  }
	
	 
}
